package com.cmdfut.rxjava;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.cmdfut.rxjava.rxjava.Disposable;
import com.cmdfut.rxjava.rxjava.Function;
import com.cmdfut.rxjava.rxjava.Observable;
import com.cmdfut.rxjava.rxjava.ObservableEmitter;
import com.cmdfut.rxjava.rxjava.ObservableOnSubscribe;
import com.cmdfut.rxjava.rxjava.Observer;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("com.cmdfut.rxjava", appContext.getPackageName());
    }

    @Test
    public void rxjavaTest() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                System.out.println("subscribe() 开始发送事件,线程:"+Thread.currentThread().getName());
                emitter.onNext(1);
                emitter.onComplete();
            }
        })
                .map(new Function<Integer, Object>() {
                    @Override
                    public String apply(Integer integer) {
                        System.out.println("apply:线程:"+Thread.currentThread().getName());
                        return integer+"啊啊啊啊啊啊";
                    }
                })
                .subscribeOn()
                .observeOn()
                //.map 以后就是 ObservableMap中的 subscribe 调用
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                     d.dispose(true);
                        boolean disposed = d.isDisposed();
                        System.out.println("订阅了么??::::"+disposed);
                        System.out.println("onSubscribe:线程:"+Thread.currentThread().getName());
                    }

                    @Override
                    public void onNext(String o) {
                        System.out.println("接收到了转换后的数据::::"+o);
                        System.out.println("onNext:线程:"+Thread.currentThread().getName());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
